
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;


public class Mapa extends Canvas {
	

	//atributo
	private int ancho;
	private int largo;
	private int posx;
	private int posy;
	private boolean pintar;
	
	// variables
	int[][] terreno = new int[10][10];
	int cont = 0;
	Image imagen;
	int hcuadro;
	int wcuadro;
	int delay;
	long time1;
	long time2;
	
	public Mapa(String codigo){
		this.setFullScreenMode(false);
		ancho = this.getWidth();
		largo = this.getHeight();
		hcuadro = largo/10;
		wcuadro = ancho/10;
		posx = 0;
		posy = 9;
		pintar=false;
		for (int j=0; j<10; j++)
			for (int i=0; i<10; i++){
				terreno[i][j]=codigo.charAt(cont)-48;
				cont++;
			}
		terreno[0][9]=1;
	}
	
	
	protected void paint(Graphics arg) {

		/*
		try{
			//arg.setClip(0,0, wcuadro, hcuadro);
			imagen = Image.createImage("/caso1.png");
		}
		catch (IOException exception){
			exception.printStackTrace();
		}
		cosa= new Sprite(imagen, 50, 50);
		cosa.defineReferencePixel(0, 0);
		cosa.setRefPixelPosition(50, 50);
		arg.drawImage(imagen, 0, 0, Graphics.LEFT | Graphics.TOP);
		
		*/
		for (int j=0; j<10; j++)
			for (int i=0; i<10; i++){
				if ((terreno[i][j]!=2) && (terreno[i][j]!=3))
					arg.setColor(255,255,255);
				else
					if(terreno[i][j]==2)
						arg.setColor(200, 200, 200);
					else
						arg.setColor(255, 0 , 0);
				arg.fillRect(i*wcuadro, j*hcuadro, wcuadro, hcuadro);
				arg.setColor(0,0,0);
				arg.drawRect(i*wcuadro, j*hcuadro, wcuadro, hcuadro);
				arg.setColor(0, 255, 0);
				arg.fillRoundRect((posx*wcuadro)+(wcuadro/3), posy*hcuadro, wcuadro/2, hcuadro/2, 40, 40);
				arg.fillRect(posx*wcuadro, (posy*hcuadro)+(hcuadro/2), wcuadro, hcuadro/2);
				
				/*
				try{
					if ((terreno[i][j]!=2) || (terreno[i][j]!=3))
						imagen = Image.createImage("/imagenes/caso1.png");
					else
						imagen = Image.createImage("/imagenes/caso"+terreno[i][j]+".png");
				}
				catch (IOException exception){
					exception.printStackTrace();
				}
				Image icono = Image.createImage(wcuadro, hcuadro);
				arg = icono.getGraphics();
				arg.setClip(i*wcuadro, j*hcuadro, wcuadro, hcuadro);
				arg.drawImage(imagen, i*largo, j*ancho, Graphics.LEFT | Graphics.TOP);
				*/
			}
		
		
	}
   protected void keyPressed(int keyC)
   {
  	  
  	  super.keyPressed(keyC);
  	 
  	 switch(keyC){
  	 	case KEY_NUM2:
  	 		if(terreno[posx][posy-1]!=2)
  	 			posy--;
  	 	break;
  	 	case KEY_NUM8:
  	 		if(terreno[posx][posy+1]!=2)
  	 			posy++;
  	 	break;
  	 	case KEY_NUM6:
  	 		if(terreno[posx+1][posy]!=2)
  	 			posx++;
  	 	break;
  	 	case KEY_NUM4:
  	 		if(terreno[posx-1][posy]!=2)
  	 			posx--;
  	 	break;
  		case KEY_NUM5:
  	 		String cod1="51545154515451545254525452545254533333333";
  	 		String cod2="541414113131344411314232444443233333423334424244242451";
  	 		lector(cod1);
  	 	break;	
  	 		
  	 }
      repaint();
  	 
   }
   public void lector(String code){
	   for(int i=0; i<code.length(); i++){
		   System.out.println("Comenzando delay");
		   time1=System.currentTimeMillis();
		   time2=time1;
		   int cont2=0;
		   while((time2-time1)<5){
			   time2=System.currentTimeMillis();
			   System.out.println(cont2);
		   }
		   cont2++;
		   switch(code.charAt(i)-48){
		   		case 1:
		  
		   			arriba();
		   		break;
		   		case 2:
		   			abajo();
		   		break;
		   		case 3:
		   			izquierda();
		   		break;
		   		case 4:
		   			derecha();
		   		break;
		   		case 5:
		   			pintar=!pintar;
		   		break;
		   }
		   repaint();
	   }
   }
   public void arriba(){
	   if(posy!=0)
		   if(terreno[posx][posy-1]!=2)
		   {
			   if(pintar){
				   terreno[posx][posy]=3;
			   }
			   posy--;
		   }
	   
   }
   protected void abajo(){
	   if(posy!=9)
		   if(terreno[posx][posy+1]!=2)
		   {
			   if(pintar){
				   terreno[posx][posy]=3;
			   }
			   posy++;
		   }
	   	   
   }
   protected void izquierda(){
	   if(posx!=0)
		   if(terreno[posx-1][posy]!=2)
		   {
			   if(pintar){
				   terreno[posx][posy]=3;
			   }
			   posx--;
		   }
	   
   }
   protected void derecha(){
	   if(posx!=9)
		   if(terreno[posx+1][posy]!=2)
		   {
			   if(pintar){
				   terreno[posx][posy]=3;
			   }
		
			   posx++;
		   }
	   
   }
	
	

}

